package edu.gyc.objecthomework; /**

 File name: SpecieWithDensity.java

 This is just a copy of Species.java in Listing 5.19
	with one additional method, density, which is passed an area
	(in square miles) and returns population/area.

 Modified by: Lew Rakocy
 email address: Lrakocy@devrycols.edu
 Date: 8/26/00
 Last changed:
 Updated for fourth edition by Brian Durney, November 2004
*/
import java.util.*;


public class SpeciesWithDensity
{
    private String name;
    private int population;
    private double growthRate;

    public void readInput()
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("What is the species' name?");
        name = keyboard.nextLine();
        System.out.println(
                      "What is the population of the species?");
        population = keyboard.nextInt();
        while (population < 0)
        {
            System.out.println("Population cannot be negative.");
            System.out.println("Reenter population:");
            population = keyboard.nextInt();
        }
        System.out.println(
                      "Enter growth rate (percent increase per year):");
	growthRate = keyboard.nextDouble();
    }

    public void writeOutput()
    {
         System.out.println("Name = " + name);
         System.out.println("Population = " + population);
         System.out.println("Growth rate = " + growthRate + "%");
    }

    /**
     Precondition: years is a nonnegative number.
     Returns the projected population of the calling object
     after the specified number of years.
    */
    public int projectedPopulation(int years)
    {
        double populationAmount = population;
        int count = years;

        while ((count > 0) && (populationAmount > 0))
        {
            populationAmount = (populationAmount +
                         (growthRate/100) * populationAmount);
            count--;
        }
        if (populationAmount > 0)
            return (int)populationAmount;
        else
            return 0;
    }

    public void set(String newName, int newPopulation, double newGrowthRate)
    {
        name = newName;
        if (newPopulation >= 0)
            population = newPopulation;
        else
        {
            System.out.println("ERROR: using a negative population.");
            System.exit(0);
        }
        growthRate = newGrowthRate;
    }

    public String getName()
    {
        return name;
    }

    public int getPopulation()
    {
        return population;
    }

    public double getGrowthRate()
    {
        return growthRate;
    }

    public boolean equals(SpeciesWithDensity otherObject)
    {
        return ((name.equalsIgnoreCase(otherObject.name))
                && (population == otherObject.population)
                && (growthRate == otherObject.growthRate));
    }

    public double density(double area)
    {
		return population/area;
	}
}
